package feg.soft.tmis.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DBConnection {
	
	private static String url = "jdbc:mysql://localhost:3306/tmis";
	private static String user = "root";
	private static String password = "admin";
	
	private DBConnection(){
		
	}
	
	static{
		try {
			// initialize class com.mysql.jdbc.Driver
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.out.println("com.mysql.jdbc.Driver--ClassNotFound\n"+e.getMessage());
		}
	}
	
	// return java.sql.Connection
	public static Connection getConnection() throws Exception{
		return DriverManager.getConnection(url, user, password);
	}
	
	// close connection, prepared-statement, result-set
	public static void close(Connection conn,PreparedStatement ps,ResultSet rs){
		try{
			if(null!=rs){
				rs.close();
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				if(null!=ps){
					ps.close();
				}
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				try{
					if(null!=conn){
						conn.close();
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
	}
	
	// close connection, prepared-statement
	public static void close(Connection conn,PreparedStatement ps){
		try{
			if(null!=ps){
				ps.close();
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				if(null!=conn){
					conn.close();
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
}
